package cn.zhx.xa.system.dao;



import cn.zhx.xa.model.dto.user.SysUserAddOrUpdateDto;
import cn.zhx.xa.model.dto.user.SysUserSearchDto;
import cn.zhx.xa.model.entity.SysUser;
import cn.zhx.xa.system.mapper.SysUserMapper;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Component;


@Component
public class SysUserDao extends ServiceImpl<SysUserMapper, SysUser> {

    public SysUser selectByUserName(String username) {
        return lambdaQuery()
                .eq(SysUser::getUsername, username)
                .eq(SysUser::getIsDeleted, 0)
                .one();
    }

    public SysUser selectById(Long uid) {
        return lambdaQuery()
                .eq(SysUser::getId, uid)
                .eq(SysUser::getIsDeleted, 0)
                .one();
    }

    public Page<SysUser> findByPage(Page<SysUser> pageInfo, SysUserSearchDto sysUserSearchDto) {
        return lambdaQuery()
                .ge(StringUtils.isNotEmpty(sysUserSearchDto.getCreateTimeBegin()), SysUser::getCreateTime, sysUserSearchDto.getCreateTimeBegin())
                .le(StringUtils.isNotEmpty(sysUserSearchDto.getCreateTimeEnd()), SysUser::getCreateTime, sysUserSearchDto.getCreateTimeEnd())
                .like(StringUtils.isNotEmpty(sysUserSearchDto.getKeyword()), SysUser::getUsername, sysUserSearchDto.getKeyword())
                .orderBy(true, true, SysUser::getCreateTime)
                .page(pageInfo);
    }

    public void updateSysUser(SysUserAddOrUpdateDto sysUserAddOrUpdateDto) {
        lambdaUpdate()
                .set(StringUtils.isNotEmpty(sysUserAddOrUpdateDto.getUsername()),SysUser::getUsername,sysUserAddOrUpdateDto.getUsername())
                .set(StringUtils.isNotEmpty(sysUserAddOrUpdateDto.getPassword()),SysUser::getPassword,sysUserAddOrUpdateDto.getPassword())
                .set(StringUtils.isNotEmpty(sysUserAddOrUpdateDto.getPhone()),SysUser::getPhone,sysUserAddOrUpdateDto.getPhone())
                .set(StringUtils.isNotEmpty(sysUserAddOrUpdateDto.getName()),SysUser::getName,sysUserAddOrUpdateDto.getName())
                .set(StringUtils.isNotEmpty(sysUserAddOrUpdateDto.getAvatar()),SysUser::getAvatar,sysUserAddOrUpdateDto.getAvatar())
                .eq(SysUser::getId,sysUserAddOrUpdateDto.getId())
                .update();
    }
}
